<?php
!function_exists('readover') && exit('Forbidden');

function updatenewinfodb($action,$subject=null,$tid=null,$pic=null){
	global $db;
	include(D_P.'data/bbscache/newinfo_config.php');
	empty($nf_shownum) && $nf_shownum = 7;
	$subject=substrs(stripslashes($subject),$nf_titlelen);
	if($action=='newpost'){
		if($subject && $tid){
			if($pic && $nf_getpic!=2){
				if(!$nf_newinfodb['pic']){
					$nf_newinfodb['pic'][]=array(
						'url' => $pic,
						'tid' => $tid,
						'title' => $subject
					);
				}else{
					if(count($nf_newinfodb['pic'])>=5){
						array_pop($nf_newinfodb['pic']);
					}
					$nf_newinfodb['pic']=array_merge(array(0=>array('url'=>$pic,'tid'=>$tid,'title'=>$subject)),$nf_newinfodb['pic']);
				}
				$nf_newinfodb=addslashes(serialize($nf_newinfodb));
				$db->pw_update(
					"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
					"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
					"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
				);
				updatecache_newinfo();
			}else{

				if(!$nf_newinfodb['new']){
					$nf_newinfodb['new']=array($tid=>$subject);
				}else{
					if(count($nf_newinfodb['new'])>=$nf_shownum){
						array_pop($nf_newinfodb['new']);
					}
					$nf_newinfodb['new']=array($tid=>$subject) + $nf_newinfodb['new'];
				}
				$nf_newinfodb=addslashes(serialize($nf_newinfodb));
				$db->pw_update(
					"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
					"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
					"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
				);
				updatecache_newinfo();
			}
		}
	}elseif($action=='newreply'){
		if($subject && $tid){
			if(!$nf_newinfodb['reply']){
				$nf_newinfodb['reply']=array($tid=>$subject);
			}else{
				if($nf_newinfodb['reply'][$tid]){
					unset($nf_newinfodb['reply'][$tid]);
				}elseif(count($nf_newinfodb['reply'])>=$nf_shownum){
					array_pop($nf_newinfodb['reply']);
				}
				$nf_newinfodb['reply']=array($tid=>$subject) + $nf_newinfodb['reply'];
			}
			$nf_newinfodb=addslashes(serialize($nf_newinfodb));
			$db->pw_update(
				"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
				"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
				"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
			);
			updatecache_newinfo();
		}
	}
}
function deletenewinfodb($tids){
	global $db;
	include(D_P.'data/bbscache/newinfo_config.php');
	foreach($nf_newinfodb as $key=>$value){
		foreach($value as $k=>$v){
			if($key=='pic'){
				if(strpos(",$tids,",",$v[tid],")!==false){
					unset($nf_newinfodb[$key][$k]);
					$update=true;
				}
			}else{
				if(strpos(",$tids,",",$k,")!==false){

					unset($nf_newinfodb[$key][$k]);
					$update=true;
				}
			}
		}
	}
	if($update){
		$nf_newinfodb=addslashes(serialize($nf_newinfodb));
		$db->pw_update(
			"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
			"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
			"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
		);
		updatecache_newinfo();
	}
}
function getfidout(){
	global $db;
	$fidout = '';
	$query = $db->query("SELECT f.fid,f.allowvisit,f.password,f.f_type,fe.forumset FROM pw_forums f LEFT JOIN pw_forumsextra fe ON fe.fid=f.fid WHERE f.type<>'category'");
	while ($rt = $db->fetch_array($query)) {
		if ($rt['f_type']=='hidden' || $rt['allowvisit'] || $rt['password'] || ($rt['forumset'] && is_array($forumset = unserialize($rt['forumset'])) && $forumset['forumsell'])) {
			$fidout .= ",'$rt[fid]'";
		}
	}
	$fidout && $fidout = substr($fidout,1);
	return $fidout;
}
function updatedigpost(){
	global $db,$timestamp;
	@include(D_P.'data/bbscache/newinfo_config.php');
	$digpost = array();
	$sqladd = '';
	(int)$nf_titlelen<1 && $nf_titlelen = 30;
	empty($nf_shownum) && $nf_shownum = 7;
	$fidout = getfidout();
	$fidout && $sqladd = " AND fid NOT IN ($fidout)";
	$query = $db->query("SELECT tid,subject FROM pw_threads WHERE ifcheck='1' $sqladd AND digest IN(1,2) ORDER BY tid DESC LIMIT 0,$nf_shownum");
	while ($rt = $db->fetch_array($query)) {
		$digpost[$rt['tid']] = substrs($rt['subject'],$nf_titlelen);
	}
	$nf_newinfodb['dig'] = $digpost;
	$nf_newinfodb = addslashes(serialize($nf_newinfodb));
	$db->pw_update(
		"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
		"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
		"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
	);
	$db->pw_update(
		"SELECT * FROM pw_hack WHERE hk_name='nf_digtime'",
		"UPDATE pw_hack SET hk_value='$timestamp' WHERE hk_name='nf_digtime'",
		"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_digtime','$timestamp')"
	);
	updatecache_newinfo();
	return $digpost;
}

function updatecache_newinfo() {
	global $db;
	$query = $db->query("SELECT * FROM pw_hack WHERE hk_name LIKE 'nf\_%'");
	$newinfodb = "<?php\r\n";
	while (@extract($db->fetch_array($query))) {
		$hk_name = preg_replace('/[^\d\w\_]/is','',$hk_name);
		if (in_array($hk_name,array('nf_order','nf_newinfodb'))) {
			$hk_value = (array)unserialize($hk_value);
		}
		$newinfodb .="\$$hk_name=".pw_var_export($hk_value).";\r\n";
	}
	$newinfodb .="\n?>";
	writeover(D_P.'data/bbscache/newinfo_config.php', $newinfodb);
}
?>
